package org.leviatan.dataharbour.jclient;

import java.util.HashMap;
import java.util.Iterator;


public class DataProtocolFactory {
	
	private String senderId;
	private String actorId;
	private String taskId;
	private String procDefId;
	private String procDefName;
	private String procDefVersion;
	private String procInstanceId;
	private HashMap<String, String> variables;
	
	public DataProtocolFactory() {
		this.variables = new HashMap<String, String>();
	}
	
	public String compileToXml() {
		String xmlResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n";
		xmlResult += "\n<request>";
		
		xmlResult += "\n\t<sender id=\"" + this.senderId + "\"/>";
		xmlResult += "\n\t<actor id=\"" + this.actorId + "\"/>";
		xmlResult += "\n\t<task id=\"" + taskId + "\" name=\"" + this.taskId +"\"/>";
		xmlResult += "\n\t<process-definition id=\"" + this.procDefId + "\" name=\"" + 
			this.procDefName +"\" version=\"" + this.procDefVersion +"\"/>";
		
		xmlResult += "\n\t<process-instance id=\"" + this.procInstanceId + "\">";
		
		String name,value;
		for (Iterator i = this.variables.keySet().iterator(); i.hasNext(); ) {
			name = i.next().toString();
			value = this.variables.get(name);
			
			// append xml
			xmlResult += "\n\t\t<var>";
			xmlResult += "\n\t\t\t<name>"+name+"</name>";
			xmlResult += "\n\t\t\t<value>"+value+"</value>";
			xmlResult += "\n\t\t</var>";
		}
		xmlResult += "\n\t</process-instance>";
		xmlResult += "\n</request>";
		
		return xmlResult;
	}
	
	public void addVariable(String name, String value) {
		this.variables.put(name, value);
	}
	
	public void removeVariable(String name) {
		this.variables.remove(name);
	}
	
	public String getActorId() {
		return actorId;
	}

	public void setActorId(String actorId) {
		this.actorId = actorId;
	}

	public String getProcDefId() {
		return procDefId;
	}

	public void setProcDefId(String procDefId) {
		this.procDefId = procDefId;
	}

	public String getProcInstanceId() {
		return procInstanceId;
	}

	public void setProcInstanceId(String procInstanceId) {
		this.procInstanceId = procInstanceId;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public HashMap<String, String> getVariables() {
		return variables;
	}

	public void setVariables(HashMap<String, String> variables) {
		this.variables = variables;
	}

	public String getSenderId() {
		return senderId;
	}

	public String getProcDefName() {
		return procDefName;
	}

	public void setProcDefName(String procDefName) {
		this.procDefName = procDefName;
	}

	public String getProcDefVersion() {
		return procDefVersion;
	}

	public void setProcDefVersion(String procDefVersion) {
		this.procDefVersion = procDefVersion;
	}

	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

}
